﻿using FastReport.DevComponents.DotNetBar;
using FastReport.DevComponents.DotNetBar.Controls;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Common.Dto;
using WinFormsApp1.TongShiWei.PigData;

namespace WinFormsApp1.SuYi.Week2WinForm
{
    public partial class AddWeighingSheetWinForm : Form
    {
        public AddWeighingSheetWinForm()
        {
            InitializeComponent();
            dataGridView2Load();
            Show();
        }
        public async void dataGridView2Load()
        {
            var date = "0000-00-00";
            if (WeighingSheet.SlaughterDate != null)
            {
                if (UsersLogin.RolesName == "管理员")
                {
                    uiTextBox5.Text = WeighingSheet.SlaughterDate.Substring(0, 10).Replace("/", "-");
                    ScaleZNum.Text = WeighingSheet.Column1;
                    date = uiTextBox5.Text;
                    var data = new
                    {
                        date
                    };
                    var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<NoData>>>(URLCommon.Url2, "/Week2Get/SlaughterEveryDayGroup", HttpClientHelper.MethodType.POST, data);
                    this.uiDataGridView1.DataSource = items.Data;
                }
                else
                {
                    MessageBox.Show("非管理员请勿操作，请联系管理员");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择副产中的时间");
                return;
            }
        }
        /// <summary>
        /// 选择磅秤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton1_Click_1(object sender, EventArgs e)
        {
            if (DepotId > 0)
            {
                uiComboBox1.DataSource = null;
                uiLabel16.Text = "选择磅秤";
                var data = new
                {
                    DepotId = DepotId,
                };
                var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<Scale>>>(URLCommon.Url2, "/Week2Get/ScaleShow", HttpClientHelper.MethodType.POST, data);
                if (items.Data.Count > 0)
                {
                    uiComboBox1.DataSource = items.Data;
                    foreach (var item in items.Data)
                    {
                        TareNuma = item.TareNum;
                    }
                    ScaleJNum.Text = Convert.ToString(Convert.ToInt32(ScaleZNum.Text) - TareNuma);
                    ScalePNum.Text = TareNuma.ToString();
                    uiComboBox1.DisplayMember = "scaleName";
                    uiComboBox1.ValueMember = "scaleId";
                    scaleIdBool = true;
                }
                else
                {
                    MessageBox.Show("该仓库无磅秤请重新选择");
                    uiButton2.Enabled = true;
                    Addnum = 0;
                    DepotId = 0;
                }
                this.Controls.Add(uiComboBox1);
            }
            else
            {
                MessageBox.Show("请先选择仓库");
                return;
            }
        }

        /// <summary>
        /// 磅秤下拉绑定
        /// </summary>
        public class Scale
        {
            public int scaleId { get; set; }
            public string scaleName { get; set; }
            public int TareNum { get; set; }
        }
        /// <summary>
        /// 选择仓库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            Addnum += 1;
            uiLabel16.Text = "选择仓库";
            uiComboBox1.DataSource = null;
            var data = new
            {

            };
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<WareHouseData>>>(URLCommon.Url2, "/Week2Get/WarehouseDataShow", HttpClientHelper.MethodType.POST, data);
            uiComboBox1.DataSource = items.Data;
            uiComboBox1.DisplayMember = "depotTypeName";
            uiComboBox1.ValueMember = "depotTypeId";

            this.Controls.Add(uiComboBox1);
        }
        public class WareHouseData
        {
            public int depotTypeId { get; set; }
            public string depotTypeName { get; set; }
        }


        //判断条件
        public int Addnum = 0;
        public int scaleId = 0;
        public bool scaleIdBool = false;

        private void uiButton9_Click(object sender, EventArgs e)
        {
            if (Addnum > 0)
            {
                DepotId = Convert.ToInt32(uiComboBox1.SelectedValue);
                Addnum--;
            }
            if (DepotId > 0)
            {
                scaleId = Convert.ToInt32(uiComboBox1.SelectedValue);
                uiButton2.Enabled = false;
            }
            if (scaleIdBool == true)
            {
                uiButton1.Enabled = false;
            }
        }
        private async void Show()
        {
            var data = new
            {

            };
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<ShowWeighingSheets>>>(URLCommon.Url2, "/Week2Get/WeighingSheetsShow", HttpClientHelper.MethodType.POST, data);
            this.WhitePorkList.DataSource = items.Data;

        }
        /// <summary>
        /// 全部 uiButton6
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton6_Click(object sender, EventArgs e)
        {
            uiButton10.Enabled = true;
            uiButton11.Enabled = true;
            uiButton12.Enabled = true;
            uiButton13.Enabled = true;
            uiButton14.Enabled = true;
        }
        /// <summary>
        /// 红脏 uiButton5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton5_Click(object sender, EventArgs e)
        {
            uiButton10.Enabled = true;
            uiButton11.Enabled = true;

            uiButton12.Enabled = false;
            uiButton13.Enabled = false;
            uiButton14.Enabled = false;
        }
        /// <summary>
        /// 白脏 uiButton8
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton8_Click(object sender, EventArgs e)
        {
            uiButton10.Enabled = false;
            uiButton11.Enabled = false;

            uiButton12.Enabled = true;
            uiButton13.Enabled = true;
            uiButton14.Enabled = true;
        }



        //添加需要的字段
        // 仓库
        public int DepotId = 0;
        // 商品代号
        public int GoodsMinuteIda = 0;
        //计划号
        public string SlaughterNo = "";
        //净重
        public int TareNuma = 0;
        private async void uiButton20_Click(object sender, EventArgs e)
        {
            AddWeighingSheets AddInfo = new AddWeighingSheets()
            {
                WeighId = 0,
                WeighDate = DateTime.Now,
                ProjectNo = SlaughterNo,
                DepotId = DepotId,
                SkinWeight = Convert.ToInt32(ScalePNum.Text),
                GrossWeight = 1,
                NetWeight = Convert.ToInt32(ScaleJNum.Text) - 1,
                WeighingSheetsNum = Convert.ToInt32(GoodsNum.Text),
                WeighingDesc = WeighingDesc.Text,
                IsDel = 0,
                GoodsMinuteId = GoodsMinuteIda
            };
            var res = await HttpClientHelper.RequestUrl<AddWeighingSheets, ApiResult<AddWeighingSheets>>(URLCommon.Url1, "/Week2Setup/AddWeighingSheet", HttpClientHelper.MethodType.POST, AddInfo);
            if (res.Code == 1)
            {
                //弹框提示
                MessageBox.Show("添加成功");
                //关闭本页面
                this.Hide();
            }
            else
            {
                //弹框提示
                MessageBox.Show("添加失败");
                //关闭本页面
                this.Hide();
            }

        }
        //红脏
        private void uiButton10_Click(object sender, EventArgs e)
        {
            GoodsMinuteIda = 5;
        }
        private void uiButton11_Click(object sender, EventArgs e)
        {
            GoodsMinuteIda = 4;
        }
        //白脏
        private void uiButton12_Click(object sender, EventArgs e)
        {
            GoodsMinuteIda = 3;
        }

        private void uiButton13_Click(object sender, EventArgs e)
        {
            GoodsMinuteIda = 2;
        }

        private void uiButton14_Click(object sender, EventArgs e)
        {
            GoodsMinuteIda = 1;
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SlaughterNo = uiDataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString();
            MessageBox.Show(SlaughterNo);
        }
    }
}
